package list;

/**
 * Created with IntelliJ IDEA.
 * User: aborisenko
 * Date: 11.01.13
 * Time: 16:54
 * To change this template use File | Settings | File Templates.
 */
public class Rotater {
    static class SimpleList {
        private int value;
        private SimpleList next;

        SimpleList(int value) {
            this.value = value;
        }

        SimpleList(int value, SimpleList next) {
            this.value = value;
            this.next = next;
        }

        @Override
        public String toString() {
            if (next == null) {
                return String.valueOf(value);
            } else {
                return value + ", " + next.toString();
            }
        }
    }

    public static void main(String[] args) {
        SimpleList head = new SimpleList(1, new SimpleList(2, new SimpleList(3, new SimpleList(4, new SimpleList(5)))));

        System.out.println(head.toString());
        // System.out.println(rotate(head, 2));
        System.out.println(printReversedList(head));
    }

    private static String printReversedList(SimpleList list){
        if(list==null) return null;
        if(list.next==null) return String.valueOf(list.value);
        return printReversedList(list.next)+", "+String.valueOf(list.value);
    }
    private static SimpleList rotate(SimpleList list, int index) {
        if (index == 0) {
            return list;
        }
        SimpleList result = new SimpleList(list.value);
        SimpleList tail = null;

        while(index>0){
             SimpleList anotherNode = list.next;
            index--;

            result.next =anotherNode;
            list=anotherNode;
        }
        return result;
    }
}
